package com.security.test.springsecuritydemo13.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2023/1/25-17:06
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Configuration
public class ProjectConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 采用OAuth2的登录认证方式
        http.oauth2Login(c -> {
            c.clientRegistrationRepository(clientRegistrationRepository());
        });

        http.authorizeRequests()
                .anyRequest()
                .authenticated();
    }

    private ClientRegistrationRepository clientRegistrationRepository() {
        var c = clientRegistration();
        return new InMemoryClientRegistrationRepository(c);
    }

    private ClientRegistration clientRegistration() {
        return CommonOAuth2Provider.GITHUB.getBuilder("github")
                .clientId("9690edbbe8bcc063430b")
                .clientSecret("e6c8cd42ea624b6cbfe68868707ea6564a8dc6db")
                .build();
    }
}
